<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use app\common\helper\FormatHelper;
use app\common\model\Users as UsersM;
use app\common\controller\Core as CoreC;
use think\Model as ThinkModel;
use think\Db;

/**
 *  提现 模型
**/
class ViperUserCash extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'qt_user_viper_cash';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     *  发起提现
    **/
    public function addUp($user_id,$cash_type,$money,$cash_num = [],$content = '')
    {

        $userInfo   = ViperUser::where('id',$user_id)->find();
        //获取提现配置
        $start_money    = config('withdrawal_start_money');   //最低提现金额
        //$mul            = config('withdrawal_mul');         //倍数
        if($money < $start_money) {
            return FormatHelper::fReturn('最低提现'.$start_money.'元','',1);
        }
        //if(($money % $start_money) != 0) {
        //    return FormatHelper::fReturn('提现金额必须是'.$start_money.' 的倍数','',1);
        //}

        if($money > $userInfo['money']) {
            return FormatHelper::fReturn('余额不足','',1);
        }

        //扣除 提现手续费
        $sxf        =   $money * config('withdrawal_sxf_rabe');
        $cashmoeny  =   $money - $sxf;

        $add['uv_id']     = $user_id;
        $add['cash_type']   = $cash_type;
        $add['money']       = $money;
        $add['sxf']         = $sxf;
        $add['actual']      = $cashmoeny;
        $add['cash_num']    = json_encode($cash_num);
        $add['content']     = $content;
        $add['create_time'] = time();

        $CoreC   =   new CoreC();
        Db::startTrans();
        try{
            $this->insert($add);
            //记录用户余额变动记录
            $CoreC->viper_add_log($user_id,$money,2,3,1,'发起提现余额扣除'.$money);
            Db::commit();
            return FormatHelper::fReturn('提现成功','');
        }catch(\Exception $e){
            Db::rollback();
            return FormatHelper::fReturn($e->getMessage(),'',1);
        }

    }


    /**
     *  提现记录
    **/
    public function getList($user_id,$limit = 10)
    {
        $list       = $this::where(['uv_id'=>$user_id])
            ->order('create_time DESC')
            ->paginate($limit)
            ->each(function($item)
            {
                $item->cash_num     = json_decode($item->cash_num,true);
                $item->create_time  = ST($item->create_time);
                return $item;
            });

        return $list->items();
    }



}